package com.project.weather.service.impl;

import com.project.weather.pojo.UserRoleBean;
import com.project.weather.service.UserRoleService;
import com.project.weather.utils.RSAEncrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.project.weather.dao.UserMapper;
import com.project.weather.pojo.UserBean;
import com.project.weather.service.UserService;
import com.project.weather.common.PageBean;
import com.project.weather.common.AResultCode;
import com.project.weather.common.ResultMap;
import com.project.weather.utils.StringUtil;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 *<p>Title:UserServiceImpl.java</p>
 *<p>Description:用户信息表ServiceImpl</p>
 *@author xxx
 *@version 1.0
 *@Automatically generate by Coder in 2021-12-21 16:07
 */
@Service
public class UserServiceImpl implements UserService{
	private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
	@Autowired
	private UserMapper userMapper;

	@Autowired
	private UserRoleService userRoleService;
	
	@Override
	public ResultMap<AResultCode, UserBean> insert(UserBean bean){
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();
		if(StringUtil.isNull(bean)){
			result.setCode(AResultCode.IS_NULL);
		} else {
//			if(StringUtil.isNull(bean.getUser_id())){
//				bean.setUser_id(StringUtil.getUUID());
//			}

			ResultMap<AResultCode, UserBean> byUserName = this.getByUserName(bean.getUsername());
			if(AResultCode.isOk(byUserName)&&byUserName.getResult()!=null){
				//用户名已存在
				result.setCode(AResultCode.AREADY_EXIST_USERNAME);
				return result;
			}


			try {
				bean.setPassword(RSAEncrypt.encrypt(bean.getPassword()));
			} catch (Exception e) {
				e.printStackTrace();
			}

			userMapper.insertSelective(bean);

			UserBean uu = userMapper.getByUserName(bean.getUsername());

			UserRoleBean userRoleBean = new UserRoleBean();
			userRoleBean.setUser_id(uu.getUser_id());

			if(!StringUtil.isNull(bean.getRole_name())){
				if("admin".equalsIgnoreCase(bean.getRole_name())){
					userRoleBean.setRole_id(2);
				}else{
					userRoleBean.setRole_id(1);
				}
			}else{
				userRoleBean.setRole_id(1);
			}


			userRoleService.insert(userRoleBean);

			result.setCode(AResultCode.OK);
			result.setResult(bean);
		}
		return result;
	}

	
	@Override
	public ResultMap<AResultCode, UserBean> update(UserBean bean){
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();

		if(StringUtil.isNull(bean)){
			result.setCode(AResultCode.IS_NULL);
		} else if(StringUtil.isNull(bean.getUser_id())){
			result.setCode(AResultCode.MISS_ID);
		} else {

			try {
				bean.setPassword(RSAEncrypt.encrypt(bean.getPassword()));
			} catch (Exception e) {
				e.printStackTrace();
			}

			userMapper.updateSelective(bean);	
			result.setCode(AResultCode.OK);
			result.setResult(bean);
		}

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, UserBean> delete(String user_id){
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();

		if(StringUtil.isNull(user_id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			String[] ids = user_id.split(",");
			userMapper.delete(ids);
			result.setCode(AResultCode.OK);
		}

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, PageBean<UserBean>> pageList(PageBean<UserBean> pb){
		ResultMap<AResultCode, PageBean<UserBean>> result = new ResultMap<AResultCode, PageBean<UserBean>>();

		List<UserBean> userBeans = userMapper.pageList(pb);

		Iterator<UserBean> iterator = userBeans.iterator();

		while (iterator.hasNext()){
			UserBean userBean = iterator.next();

			if(userBean.getUsername().equalsIgnoreCase("superAdmin")){
				iterator.remove();
			}

			try {
				userBean.setPassword(RSAEncrypt.decrypt(userBean.getPassword()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

//		for(UserBean userBean : userBeans){
//			try {
//				userBean.setPassword(RSAEncrypt.decrypt(userBean.getPassword()));
//			} catch (Exception e) {
//				e.printStackTrace();
//			}
//		}


		pb.setData(userBeans);
		pb.clear();

		result.setCode(AResultCode.OK);
		result.setResult(pb);

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, List<UserBean>> list(UserBean bean){
		ResultMap<AResultCode, List<UserBean>> result = new ResultMap<AResultCode, List<UserBean>>();

		List<UserBean> list = userMapper.list(bean);

		for(UserBean userBean : list){
			try {
				userBean.setPassword(RSAEncrypt.decrypt(userBean.getPassword()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		result.setCode(AResultCode.OK);
		result.setResult(list);

		return result;
	}

	
	@Override
	public ResultMap<AResultCode, UserBean> get(String user_id){
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();
		if(StringUtil.isNull(user_id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			UserBean bean = userMapper.get(user_id);
			if(bean != null) {

				try {
					bean.setPassword(RSAEncrypt.decrypt(bean.getPassword()));
				} catch (Exception e) {
					e.printStackTrace();
				}

				result.setCode(AResultCode.OK);
				result.setResult(bean);
			} else {
				result.setCode(AResultCode.UNEXIST);
	 		}
		}
		return result;
	}

	@Override
	public ResultMap<AResultCode, UserBean> getByUserNamePassword(String username, String password) {
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();
		if(StringUtil.isNull(username)||StringUtil.isNull(password)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			UserBean bean = userMapper.getByUserNamePassword(username,password);
			if(bean != null) {
				result.setCode(AResultCode.OK);
				result.setResult(bean);
			} else {
				result.setCode(AResultCode.UNEXIST);
			}
		}
		return result;
	}

	@Override
	public ResultMap<AResultCode, UserBean> getByUserName(String username) {
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();
		if(StringUtil.isNull(username)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			UserBean bean = userMapper.getByUserName(username);
			if(bean != null) {
				result.setCode(AResultCode.OK);
				result.setResult(bean);
			} else {
				result.setCode(AResultCode.UNEXIST);
			}
		}
		return result;
	}

	@Override
	public ResultMap<AResultCode, UserBean> forbid(String user_id) {
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();

		if(StringUtil.isNull(user_id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			String[] ids = user_id.split(",");
			userMapper.forbid(ids);
			result.setCode(AResultCode.OK);
		}

		return result;
	}

	@Override
	public ResultMap<AResultCode, UserBean> start(String user_id) {
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();

		if(StringUtil.isNull(user_id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			String[] ids = user_id.split(",");
			userMapper.start(ids);
			result.setCode(AResultCode.OK);
		}

		return result;
	}

	@Override
	public ResultMap<AResultCode, UserBean> reliveAdmin(String user_id) {
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();

		if(StringUtil.isNull(user_id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			String[] ids = user_id.split(",");
			userMapper.reliveAdmin(ids);
			result.setCode(AResultCode.OK);
		}

		return result;
	}

	@Override
	public ResultMap<AResultCode, UserBean> grantAdmin(String user_id) {
		ResultMap<AResultCode, UserBean> result = new ResultMap<AResultCode, UserBean>();

		if(StringUtil.isNull(user_id)){
			result.setCode(AResultCode.MISS_ID);
		} else {
			String[] ids = user_id.split(",");
			userMapper.grantAdmin(ids);
			result.setCode(AResultCode.OK);
		}

		return result;
	}


}